package com.synergy.stockflow.filter;

import org.springframework.lang.NonNull;
import org.springframework.web.filter.OncePerRequestFilter;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * <h2>自定义过滤器基类，用于放行预检请求OPTIONS</h2>
 * <p>
 * 业务中创建过滤器时基础当前类
 * </p>
 */
public abstract class BaseCorsRequestFilter extends OncePerRequestFilter {
    @Override
    protected final void doFilterInternal(HttpServletRequest request, @NonNull HttpServletResponse response, @NonNull FilterChain filterChain)
            throws ServletException, IOException {

        if ("OPTIONS".equalsIgnoreCase(request.getMethod())) {
            filterChain.doFilter(request, response);
            return;
        }

        doFilterCustom(request, response, filterChain);
    }

    protected abstract void doFilterCustom(HttpServletRequest request, HttpServletResponse response,
                                           FilterChain filterChain) throws ServletException, IOException;
}
